/*
 *
 * Copyright (c) 2007, LilyStudio Nanjing University.
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *  * Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *  * Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *  * Neither the name of LilyStudio Nanjing University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package org.lilystudio.veryftp.dao.impl;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.lilystudio.veryftp.dao.FTPFileDAO;
import org.lilystudio.veryftp.model.FTPFile;
import org.springframework.orm.hibernate3.HibernateCallback;

/**
 * 
 * @author Goodgtiger
 * @version Oct 25, 2007
 */
public class FTPFileDAOImpl extends GenericSpringHibernateDAO<FTPFile, Long>
		implements FTPFileDAO {

	// cjf：下面的外部配置定义获取有一些问题，PropertiesUtil类对Properties.properties文件的获取路径是假设在Web容器下面的
	// public static final int MAX_RESULTS = Integer.parseInt(new
	// PropertiesUtil().getProperty("index_num_per"));
	int MAX_RESULTS = 2;// 此处只为调试

	// 此处的@SuppressWarnings("unchecked"),是eclipse的提示加上的，不知道可不可以去掉。
	/**
	 * 
	 */
	@SuppressWarnings("unchecked")
	public List<FTPFile> getFTPFiles(final int indexState) {
		return (List<FTPFile>) getHibernateTemplate().execute(
				new HibernateCallback() {
					public Object doInHibernate(final Session session)
							throws HibernateException {
						Criteria criteria = getSession().createCriteria(
								getPersistentClass());
						criteria.setProjection(null).add(
								Restrictions.eq("fileState", Integer
										.valueOf(indexState))).setMaxResults(
								MAX_RESULTS);
						return criteria.list();
					}
				}, true);
	}

	/**
	 * 
	 */
	public int getFTPFilesCount(final int indexState) {
		Integer count =  (Integer) getHibernateTemplate().execute(
				new HibernateCallback() {
					public Object doInHibernate(Session session)
							throws HibernateException {
						Criteria criteria = getSession().createCriteria(
								getPersistentClass());
						return criteria.setProjection(Projections.rowCount())
								.add(
										Restrictions.eq("fileState", Integer
												.valueOf(indexState)))
								.uniqueResult();
					}
				}, true);
		return count.intValue();

	}

	/**
	 *	这里用了HQL，暂时没有更好的方法。
	 *	这个方法是在 一种索引状态的数据“全部”被改变为另外一种状态后，将数据库中的数据状态一次性更改
	 */
	public void changeFTPFileState(int oldState, int newState) {
		String hql = "update FTPFile set fileState = " + newState + " where fileState = " + oldState;
		Query query = getSession().createQuery(hql);
		query.executeUpdate();
	}

}
